package com.markerhub.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysFood;
import com.markerhub.excel.FoodListener;
import com.markerhub.service.SysFoodService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author jsx on 2023/2/25
 **/
@RestController
@Slf4j
@RequestMapping("/sys/food")
public class FoodController extends BaseController {

    @Autowired
    private SysFoodService foodService;

    //增加
    @PostMapping("/save")
    public Result save(@RequestBody SysFood sysFood){
        foodService.save(sysFood);
        return Result.succ("保存完成");
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody SysFood sysFood){
        foodService.updateById(sysFood);
        return Result.succ("修改完成");
    }

    //删除
    @GetMapping("/delete")
    public Result delete(String id){
        foodService.removeById(id);
        return Result.succ("删除完成");
    }

    //分页查询
    @GetMapping("/list")
    public Result list(String name,String sick,Integer current,Integer size){
        QueryWrapper<SysFood> wrapper = new QueryWrapper();
        if( name != null && name !="" ){
            wrapper.like("name",name);
        }
        if(sick != null && sick !=""){
            wrapper.like("sick",sick);
        }
        Page<SysFood> list = foodService.page(getPage(current,size),wrapper);
        return Result.succ(list);
    }

    //批量导入数据
    //上传文件
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping("/load")
    public Result load(MultipartFile file){
        if(file == null){
            return Result.fail("请先上传文件");
        }
        try {
            EasyExcel.read(file.getInputStream(), SysFood.class, new FoodListener(foodService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("上传失败");
        }
        return Result.succ("上传成功");
    }
    //查询单条记录
    @GetMapping("/info/{id}")
    public Result info(@PathVariable(value = "id") String id){
        SysFood food = foodService.getById(id);
        return Result.succ(food);
    }

    //模板下载
    @GetMapping("/download")
    public void Download(HttpServletResponse response){
        try {

            // 以流的形式下载文件。
            String filename = "食物信息模板";
            InputStream fis = FoodController.class.getResourceAsStream("/excel/食物信息模板.xlsx");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(filename + ".xlsx",  "UTF-8"));
            response.addHeader("Content-Length", "" + buffer.length);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.addHeader(  "Access-Control-Allow-Origin","*");//允许所有来源访同
            response.addHeader(  "Access-Control-Allow-Method","POST,GET");//允许访问的方式

            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            log.error("系统有误");
        }

    }

    //计算卡路里

    //计算营养值


}
